package org.utils.util;

import java.util.Enumeration;

/**
 * Title:        $title$
 * Description:  $description$
 * Copyright:    Copyright (c) 2002
 * Company:      $company$
 * @author $author$
 * @version $version$
 */

public class ValueGenerator
{
  public static String PATTERN = "${random}";
  
  public ValueGenerator()
  {
  }

  public StringBuffer generate(BufferInformation info, Enumeration values) throws Exception
  {
  	return generate(new StringBuffer(), info, values);
  }

  public StringBuffer generate(StringBuffer buffer, BufferInformation info, Enumeration values) throws Exception
  {
    buffer.append( info.getHeader() );
    while (values.hasMoreElements())
    {
      String value = (String) values.nextElement();
      String line = info.getLine();
      int pos = -1;
      while ((pos = line.indexOf(PATTERN)) >= 0)
      {
        buffer.append(line.substring(0, pos));
        buffer.append(value);
        line = line.substring(pos+PATTERN.length());
      }
      buffer.append(line);
    }
    buffer.append( info.getFooter() );
    return buffer;
  }
  
  /**
   * @author Manuel EVENO
   */
  public static class BufferInformation
  {
  	private String header = null;
  	private String footer = null;
  	private String line = null;
    /**
     * Constructor for BufferInformation.
	 */
	public BufferInformation()
	{
		super();
	}
	/**
	 * Returns the footer.
	 * @return String
	 */
	public String getFooter()
	{
		return footer;
	}

	/**
	 * Returns the header.
	 * @return String
	 */
	public String getHeader()
	{
		return header;
	}

	/**
	 * Returns the line.
	 * @return String
	 */
	public String getLine()
	{
		return line;
	}

	/**
	 * Sets the footer.
	 * @param pFooter The footer to set
	 */
	public void setFooter(String pFooter)
	{
		this.footer = pFooter;
	}

	/**
	 * Sets the header.
	 * @param pHeader The header to set
	 */
	public void setHeader(String pHeader)
	{
		this.header = pHeader;
	}

	/**
	 * Sets the line.
	 * @param pLine The line to set
	 */
	public void setLine(String pLine)
	{
		this.line = pLine;
	}
  }
}